import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import multer from 'multer';
import fs from 'fs';
import mammoth from 'mammoth';
import path from 'path';
const app = express();
app.use(bodyParser.json());
// app.use(cors())


const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'uploads/')
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname)
    }
})
const upload = multer({ storage: storage })
app.post('/upload', upload.single('file'), async (req, res) => {
    const filePath = req.file.path;
    const result = await mammoth.convertToHtml({ path: filePath });
    const body = `<!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Document</title>
        </head>
        <body>`
    const end = `</body></html>`
    fs.writeFileSync('output.html', body + result.value + end)
    res.setHeader('Content-Type', 'text/html')
    const sendPath = path.join(process.cwd(), 'output.html')
    res.sendFile(sendPath, () => {
        fs.unlinkSync(filePath)
        fs.unlinkSync('output.html')
    })
})
// const bb = ()=>{
//     console.log('bb')
// }
// jsonp
app.get('/get', (req, res) => {
    const callback = req.query.cb;
    res.setHeader('Content-Type', 'text/javascript')
    let data = [
        {
            name: 'a',
            id: 1
        },
        {
            name: 'a',
            id: 1
        }
    ]
    let resData = `${callback}(${JSON.stringify(data)})`
    console.log(resData)
    res.send(`${callback}(${JSON.stringify(data)})`);
});
app.listen(1111, () => {
    console.log('server is running on port 1111');
})